/// @Classname : HcToast
/// @Author :
/// @Time : 2024-02-27 16:10
/// @Description : //土司展示工具类
part of hc_toast_util;

class _HcToast {
  OverlayState overlayState;
  OverlayEntry overlayEntry;
  bool _isVisible = false;

  _HcToast({required this.overlayState, required this.overlayEntry});

  void show({required Duration duration, VoidCallback? onDismiss}) {
    _isVisible = true;
    overlayState.insert(overlayEntry);
    //如果等于0就延迟关闭 等于0就不关闭
    if (duration.inMicroseconds != 0) {
      Future.delayed(duration, () {
        hide(onDismiss: onDismiss);
      });
    }
  }

  void hide({VoidCallback? onDismiss}) {
    if (!_isVisible) return;
    _isVisible = false;
    overlayEntry.remove();
    onDismiss?.call();
  }
}

class _HcToastContainer extends StatelessWidget {
  final String text;
  final Widget? icon;
  final HcToastPosition position;
  final bool showMask;
  final HcToastTheme? themeData;
  final Color? textColor;

  /// 构建Toast
  /// [text]提示文字
  /// [icon]Widget组件
  /// [position] 提示位置
  /// [showMask] 是否阻止穿透
  /// [themeData] 主题配置
  /// [textColor]文字颜色
  const _HcToastContainer({
    required this.text,
    this.icon,
    required this.position,
    required this.showMask,
    this.themeData,
    this.textColor,
  });

  @override
  Widget build(BuildContext context) {
    HcToastTheme theme = themeData ?? HcToastTheme();
    theme = HcCustomerConfig().allTheme.toastTheme.merge(theme);
    Color tColor = textColor ??
        (HcColorUtil.needLightColor(theme.background)
            ? theme.commonTheme.textInverseColor
            : theme.commonTheme.textBaseColor);
    EdgeInsetsGeometry? verticalMargin;
    Alignment? alignment;

    switch (position) {
      case HcToastPosition.top:
        verticalMargin = EdgeInsets.only(
            top: theme.verticalMargin + MediaQuery.of(context).viewInsets.top);
        alignment = Alignment.topCenter;
        break;
      case HcToastPosition.bottom:
        verticalMargin = EdgeInsets.only(
            bottom: theme.verticalMargin +
                MediaQuery.of(context).viewInsets.bottom);
        alignment = Alignment.bottomCenter;
        break;
      default:
        verticalMargin = EdgeInsets.zero;
        alignment = Alignment.center;
        break;
    }
    //是否要阻止穿透
    return IgnorePointer(
      ignoring: !showMask,
      //插入OverlayEntry防止没有Material的情况发生
      child: Material(
        color: Colors.transparent,
        //控制位置为上/下时候对顶部距离的控制
        child: Container(
          width: MediaQuery.of(context).size.width,
          padding: verticalMargin,
          alignment: alignment,
          child: Container(
            constraints: BoxConstraints(
                minWidth: icon != null ? theme.minWidth : theme.textMinWidth,
                minHeight: icon != null ? theme.minHeight : 0.0),
            margin: theme.margin,
            padding: icon != null ? theme.padding : theme.textPadding,
            decoration: BoxDecoration(
                color: theme.background,
                borderRadius: BorderRadius.circular(theme.borderRadius)),
            child: HcIconText(
              spacing: theme.spacing,
              direction: Axis.vertical,
              icon: icon != null
                  //没有设置颜色时候跟随文字颜色 随背景色而变化
                  ? IconTheme(
                      data: IconThemeData(color: tColor, size: theme.iconSize),
                      child: icon!,
                    )
                  : null,
              label: Text(
                text,
                textAlign: TextAlign.center,
                style: TextStyle(
                    color: tColor, fontSize: theme.fontSize, height: 1.4),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
